﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-05-16 17:35:18
 * 功能：游戏入口
**************************************************************************************/

using Game.Framework;
using UnityEngine;

public class Entry : MonoBehaviour
{
    private void Awake()
    {
        StartCoroutine(FrameworkManager.Instance.Init(BaseModuleInitProgress));
        FrameworkManager.Instance.CustomMuduleInitProgressEvent += CustomModuleInitProgress;
    }

    private void BaseModuleInitProgress(float progress)
    {
        if (progress >= 1)
        {
            // 初始化原生消息接收器
            new GameObject("NativeMsgReceiver").AddComponent<NativeMsgReceiver>();

            //UIUtility.DisplayPanel(Game.UI.NetTipPanle.NetTipPanleView.PANEL_KEY, "UI/NetTipPanle/Prefabs/NetTipPanle", UIRoot.MostTop);
            // 创建Loading界面
            UIUtility.DisplayPanel(Game.UI.LoadingPanel.LoadingPanelView.PANEL_KEY, "UI/LoadingPanel/Prefabs/LoadingPanel");
        }
    }

    private void CustomModuleInitProgress(float progress)
    {
        LogManager.Log($"custom module progress:{progress}", "Entry");
        if (progress >= 1)
        {
            // 初始化音效管理器
            AudioManager.Instance.ChangeBgVolume(GameDataMgr.Instance.MusicSwitch ? BaseDataMgr.Instance.Cfg.bgVolume : 0);
            AudioManager.Instance.ChangeAudioVolume(GameDataMgr.Instance.SoundSwitch ? 1 : 0);

            // 初始化广告
            AdMgr.Instance.Init();

            // 初始化购买模块
            //bool isThree = false;
            //if (BaseDataMgr.Instance.Cfg.iap.cent > 0)
            //    isThree = BaseDataMgr.Instance.Cfg.iap.cent == 100 ? true : UnityEngine.Random.Range(0, 101) >= BaseDataMgr.Instance.Cfg.iap.cent;
            //GameObject obj = new GameObject("Purchase");
            //GameDataMgr.Instance.Purchase = isThree ? obj.AddComponent<Three>() : obj.AddComponent<IAP>();

            // 新手引导
            UIUtility.DisplayPanel(Game.UI.GuidePanel.GuidePanelView.PANEL_KEY, "UI/GuidePanel/Prefabs/GuidePanel", UIRoot.Top);
        }
    }

    private void OnDestroy()
    {
        FrameworkManager.Instance.Clear();
    }

    private void OnApplicationQuit()
    {
        GameDataMgr.Instance.Save();
    }
}